home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / admin-v1.000 / admin-v1 / checkusers < prev    next >
Text File  |  1995-05-20  |  5KB  |  173 lines

  1. #!/usr/bin/perl
  2. #
  3. # Program to check to see if users exist
  4. # Version 1.00 by Ken Hollis
  5. #
  6. # THIS PROGRAM MUST BE RUN AS SUPERUSER IF YOU WANT IT TO DELETE THE
  7. # DIRECTORIES FOR YOU AUTOMATICALLY FROM THE USER HOME DIRECTORY!
  8. #
  9. # The search method used in this program is very slow.  I will devise
  10. # a different way to get this working, but for the time being, it works.
  11. #
  12. # Update log:
  13. #    01/25/95: Started the program
  14. #    01/26/95: Fixed a bug in directory checking, tuned things up
  15.  
  16. ### Change this if it's different on your system
  17. $HOME_DIR = "/home/users";
  18.  
  19. ### This was "ls.old" on our machine, it may be "ls" on yours.
  20. ### This was because of ANSI color graphics in the listing otherwise.
  21. $LS_COMMAND = "/bin/ls -al";
  22.  
  23. ### Cut from byte-order 56 to the end of line
  24. $CUT_PARSE = "cut -b56-";
  25.  
  26. ### Display mode (V = Verbose (for debugging), S = Standard, Q = Quiet)
  27. $DISP_MODE = "V";
  28.  
  29. ### Auto-delete flag (TRUE = Auto-delete non-matched directories, or
  30. ### FALSE = Just list them, no deletion)
  31. $AUTO_DELETE = "TRUE";
  32.  
  33. sub check_options {
  34.     if ($HOME_DIR eq "") {
  35.     print "Sorry, cannot have a null home directory.  If you intended\n";
  36.     print "for this to be root, please put a forward slash (\"/\") in\n";
  37.     print "the \$HOME_DIR field.\n\n";
  38.     die "Configuration Error in HOME_DIR variable\n";
  39.     } elsif ($LS_COMMAND eq "") {
  40.     print "Sorry, there is no directory listing command.  Since there\n";
  41.     print "is no default, the program will break here.\n\n";
  42.     die "Configuration Error in LS_COMMAND variable\n";
  43.     } elsif ($CUT_PARSE eq "") {
  44.     print "Sorry, there is no cut-parsing command.  Make sure your\n";
  45.     print "computer system can handle a parse command like \"cut\".\n";
  46.     print "If it cannot, do NOT use this program!\n\n";
  47.     die "Configuration Error in CUT_PARSE variable\n";
  48.     } elsif ($DISP_MODE eq "") {
  49.     print "Sorry, there is a display mode configuration error.  Since\n";
  50.     print "there is no default value if this is unfilled, you must edit\n";
  51.     print "this variable, as was given in the documentation.\n\n";
  52.     die "Configuration Error in DISP_MODE variable\n";
  53.     } elsif ($AUTO_DELETE eq "") {
  54.     print "Sorry, there is no AUTO_DELETE variable set, or you have not\n";
  55.     print "configured it correctly.  Please go back and check this\n";
  56.     print "variable, as per documentation.\n\n";
  57.     die "Configuration Error in AUTO_DELETE variable\n";
  58.     } else {
  59.     if ($DISP_MODE eq "V") {
  60.         print "check_options passed\n";
  61.     }
  62.     }
  63. }
  64.  
  65. sub initialize_dir {
  66.     if ($DISP_MODE eq "V") {
  67.     print "Initializing directory display with ${LS_COMMAND} | ${CUT_PARSE}\n";
  68.     }
  69.  
  70.     `${LS_COMMAND} ${HOME_DIR} | ${CUT_PARSE} > ./dir_file`;
  71.  
  72.     if ($DISP_MODE eq "V") {
  73.     print "Done initializing directory file (file is /dir_file)\n";
  74.     } elsif ($DISP_MODE eq "S") {
  75.     print "Done.\n";
  76.     }
  77. }
  78.  
  79. sub check_loop {
  80.     $f_user = 0;
  81.     $n_user = 0;
  82.  
  83.     open(DIN, "./dir_file") || die "Cannot open ./dir_file: $!\n";
  84.     while(<DIN>) {
  85.     $umatch = $_;
  86.     chop($umatch);
  87.  
  88.     ### For some reason, umatch gets lost, so we save it here.
  89.     $username = $umatch;
  90.  
  91.     if (($DISP_MODE eq "V") || ($DISP_MODE eq "S")) {
  92.         if (($username ne "") && ($username ne ".") && ($username ne "..") &&
  93.         ($username ne "lost+found") && ($username ne "news")) {
  94.         print "User $umatch ";
  95.         }
  96.     }
  97.  
  98.     $found = "FALSE";
  99.  
  100.     open(PIN, "/etc/passwd") || die "Cannot find /etc/passwd: $!\n";
  101.     while(<PIN>) {
  102.         ($uname, $upass, $uuid, $ugid, $ufullname, $udir, $ushell) = split(/:/);
  103.  
  104.         $udir =~ s/^${HOME_DIR}\///;
  105.  
  106.         if (($umatch eq "") || ($umatch eq ".") || ($umatch eq "..") ||
  107.         ($umatch eq "lost+found") || ($umatch eq "news")) {
  108.         last;
  109.         }
  110.  
  111.         if ($udir eq $umatch) {
  112.         $found = "TRUE";
  113.         last;
  114.         } else {
  115.         $found = "FALSE";
  116.         }
  117.     }
  118.     close(PIN);
  119.  
  120.     if (($username ne "") && ($username ne ".") && ($username ne "..") &&
  121.         ($username ne "lost+found") && ($username ne "news")) {
  122.         if ($found eq "TRUE") {
  123.         $f_user++;
  124.  
  125.         if ($DISP_MODE ne "Q") {
  126.             print "found ";
  127.         }
  128.  
  129.         if ($DISP_MODE eq "V") {
  130.             print "[$uuid | $ugid]";
  131.         }
  132.  
  133.         if ($DISP_MODE ne "Q") {
  134.             print "\n";
  135.         }
  136.         } else {
  137.         $n_user++;
  138.  
  139.         if ($DISP_MODE ne "Q") {
  140.             print "not found ... ";
  141.         }
  142.  
  143.         if ($AUTO_DELETE eq "TRUE") {
  144.             if ($DISP_MODE eq "V") {
  145.             print "rm -rf ${HOME_DIR}/${username}\n";
  146.             }
  147.             system ("rm -rf ${HOME_DIR}/${username}");
  148.             if ($DISP_MODE ne "Q") {
  149.             print "directory removed.";
  150.             }
  151.         }
  152.  
  153.         if ($DISP_MODE ne "Q") {
  154.             print "\n";
  155.         }
  156.         }
  157.     }
  158.     }
  159.     close(DIR_IN);
  160.  
  161.     if ($DISP_MODE eq "V") {
  162.     print "Found users: ${f_user}\nNot found: ${n_user}\n";
  163.     }
  164. }
  165.  
  166. sub main_part {
  167.     &check_options;
  168.     &initialize_dir;
  169.     &check_loop;
  170. }
  171.  
  172. &main_part;
  173.